package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImp implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    /**
     * 员工列表查询
     *
     * @param name
     * @param gender
     * @param begin
     * @param end
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageBean pageQuery(String name, Integer gender, LocalDate begin, LocalDate end, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        Page<Emp> p = empMapper.list(name, gender, begin, end);
        long total = p.getTotal();
        List<Emp> rows = p.getResult();
        return new PageBean(total, rows);
    }

    @Override
    public void batchRemoveEmp(List<Integer> ids) {
        empMapper.batchRemoveEmp(ids);
    }

    @Override
    public Emp queryId(Integer id) {
        Emp emp=empMapper.queryById(id);
        return emp;
    }

    //更新操作
    @Override
    public void modifyEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateEmp(emp);
    }

    //新增操作
    @Override
    public void addEmp(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insertEmp(emp);
    }
}
